package com.geeker.module.sys.config.service;

import cn.hutool.core.convert.Convert;
import com.geeker.common.constant.RedisKeyPrefixConst;
import com.geeker.common.core.redis.RedisCache;
import com.geeker.common.exception.BusinessException;
import com.geeker.module.sys.config.domain.SysConfig;
import com.geeker.module.sys.config.mapper.SysConfigMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

@Service
public class SysConfigService extends ServiceImpl<SysConfigMapper, SysConfig> {

    @Autowired
    private RedisCache redisCache;

    public String selectConfigByKey(String configKey) {
        String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(configKey)));
        if (StringUtils.isNotEmpty(configValue)) {
            return configValue;
        }
        List<SysConfig> list = this.list(QueryWrapper.create().eq(SysConfig::getConfigKey, configKey));
        if (CollectionUtils.isNotEmpty(list)) {
            SysConfig sysConfig = list.getFirst();
            redisCache.setCacheObject(getCacheKey(configKey), sysConfig.getConfigValue());
            return sysConfig.getConfigValue();
        }
        return StringUtils.EMPTY;
    }

    public boolean add(SysConfig config) {
        List<SysConfig> list = this.list(QueryWrapper.create().eq(SysConfig::getConfigKey, config.getConfigKey()));
        if (CollectionUtils.isNotEmpty(list)) {
            throw new BusinessException("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        boolean save = this.save(config);
        if (save) {
            redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
        }
        return save;
    }

    private String getCacheKey(String configKey) {
        return RedisKeyPrefixConst.SYS_CONFIG_KEY + configKey;
    }

    public boolean edit(SysConfig config) {
        List<SysConfig> list = this.list(QueryWrapper.create().eq(SysConfig::getConfigKey, config.getConfigKey()));
        if (CollectionUtils.isNotEmpty(list) && !Objects.equals(list.getFirst().getConfigId(), config.getConfigId())) {
            throw new BusinessException("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        boolean b = this.updateById(config);
        if (b) {
            redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
        }
        return b;
    }

    public void delete(List<Long> configIds) {
        for (Long configId : configIds) {
            SysConfig config = this.getById(configId);
            if (config == null)
                throw new BusinessException("参数错误");
            if ("Y".equals(config.getConfigType()))
                throw new BusinessException(String.format("内置参数【%1$s】不能删除 ", config.getConfigKey()));
            this.removeById(configId);
            redisCache.deleteObject(getCacheKey(config.getConfigKey()));
        }
    }

    public void resetConfigCache() {
        Collection<String> keys = redisCache.keys(RedisKeyPrefixConst.SYS_CONFIG_KEY + "*");
        redisCache.deleteObject(keys);

        List<SysConfig> configsList = this.list();
        for (SysConfig config : configsList) {
            redisCache.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
        }
    }
}
